package com.queclink.ycm.entity.data;

import com.alibaba.fastjson.JSONObject;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.common.utils.StringUtil;


/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-24 17:30:00
 */
public class Data102 extends Data {
    private String Code;
    private String SubCode;

    private String SequenceNumber;


    public Data102() {
    }

    public String getCode(JSONObject jsonObject) {
        return StringUtil.padZeroesToTheLeft(jsonObject.getString("Code"), 2);
    }

    public String getSubCode(JSONObject jsonObject) {
        return StringUtil.padZeroesToTheLeft(jsonObject.getString("SubCode"), 2);
    }

    public String getSequenceNumber(JSONObject jsonObject) {
        if (jsonObject.containsKey("SequenceNumber")) {
//            Integer sequenceNumber = jsonObject.getInteger("SequenceNumber");
            String sequenceNumber = jsonObject.getString("SequenceNumber");
            return StringUtil.padZeroesToTheLeft(sequenceNumber, 4);
        } else {
            return "";
        }
    }



    public String getDataID() {
        // data101
        return HexUtils.intToHex(102, 2);
    }

    public String getLength() {
        return "16";
    }

    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();

        sb.append(this.getCode(jsonObject));
        sb.append(this.getSubCode(jsonObject));
        sb.append("000000");
//        sb.append("0000");
        sb.append(this.getSequenceNumber(jsonObject));

        int len = sb.length();
        len = len / 2;
        if (len > 127) {
            sb.insert(0, HexUtils.intToHex(len, 4));
        } else {
            sb.insert(0, HexUtils.intToHex(len, 2));
        }
        sb.insert(0, this.getDataID());
        return sb.toString();
    }

}
